using System;
using System.Windows.Forms;
using System.Diagnostics;

namespace WillStott.MxAppToolbox
{
	/// <summary>
	/// Summary description for ProcessOps.
	/// </summary>
	public class ProcessOps
	{


		public static bool IsAppRunning(int PID)
		{
			bool rc = false;
			try
			{
				Process process = Process.GetProcessById(PID);
				rc = (process.HasExited == true) ? false : true;
			}
			catch(Exception)
			{
			}
			return rc;
		}

		public static void TerminateApp (int PID, bool HasWindow)
		{
			try
			{
				Process process = Process.GetProcessById(PID);
				if (  HasWindow == false)
					process.Kill();		//no main window
				else
				{
					process.CloseMainWindow();
					process.Close();
				}
				System.Threading.Thread.Sleep(500);

			}
			catch(Exception)
			{
			}
		}

		public static int StartApp (string ExeFile, string arg, bool HasWindow)
		{
			int rc = 0;
			try
			{
				ProcessStartInfo startInfo = new ProcessStartInfo();

				startInfo.FileName = ExeFile;
				startInfo.Arguments = (arg == null) ? "" : arg;
				startInfo.WindowStyle = (HasWindow) ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden;
			
				Process process = new Process();
				process.StartInfo = startInfo;
				process.Start();

				System.Threading.Thread.Sleep(500);

				rc = process.Id;
			}
			catch(Exception)
			{
			}
			return rc;
		}

        public static int StartApp(string file, string arg, string verb, bool HasWindow)
        {
            int rc = 0;
            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();

                startInfo.FileName = file;
                startInfo.Arguments = (arg == null) ? "" : arg;
                startInfo.Verb = verb;
                startInfo.CreateNoWindow = (HasWindow) ? false : true;
                startInfo.WindowStyle = (HasWindow) ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden;

                Process process = new Process();
                process.StartInfo = startInfo;
                process.Start();

                System.Threading.Thread.Sleep(500);

                rc = process.Id;
            }
            catch (Exception)
            {
            }
            return rc;
        }

        public static Process StartCmdLineApp(string exeFile, string workingDir, string arg)
        {
            Process process = null;
            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();

                startInfo.WorkingDirectory = workingDir;
                startInfo.FileName = exeFile;
                startInfo.Arguments = (arg == null) ? "" : arg;
                startInfo.UseShellExecute = false;
                startInfo.RedirectStandardError = true;
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardInput = true;
                startInfo.CreateNoWindow = true;
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;

                process = new Process();
                process.StartInfo = startInfo;
                process.Start();
              
            }
            catch (Exception)
            {
            }
            return process;
        }
	}
}

